﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon.Validators;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCB.UI.Utils;

namespace Baosight.ZNH.ZMCB.UI.PopWindows
{
    public partial class FirstLogin : EFViewCmes
    {
        //操作模式包括新增和更新
        private new OperationMode Mode
        {
            get;
            set;
        }
        public FirstLogin()
        {
            InitializeComponent();
        }
        
        #region 公共方法

        /// <summary>
        /// 批量录入保存的公共方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveClick(EFRegion region,string groupName, EFGrid grid,EFDataField name, EFDataField no, string pageName)
        {
            bool result = false;
            //ValidatorManager自定义的校验方法
            ValidatorManager group = this.GetChildObject<ValidatorManager>(groupName);
            if (group != null)
            {
                result = group.IsValidSucces;
            }

            bool _validateField = result;//确定前的效验

            if (!_validateField)
            {
                this.ValidateMessagePopup();
                this.TempButtonOpen(true);//确定按钮生效
            }
            else
            {
                if (Mode == OperationMode.Insert)
                {
                    if (!NoAndNameCheck(grid, name, no, pageName, OperationMode.Insert))
                    {
                        return;
                    }
                }
                else
                {
                    if (!NoAndNameCheck(grid, name, no, pageName, OperationMode.Update))
                    {
                        return;
                    }
                }

                EIRow row = new EIRow();
                foreach (var _field in region.GetChildObjects<EFDataField>(null, true))
                {
                    row.Add(_field.eBindingName, _field.Value);
                }
                if (Mode == OperationMode.Insert)
                {
                    grid.EIBlock.AddRow(row);
                }
                else if (Mode == OperationMode.Update)
                {
                    int _index = grid.SelectedIndex;
                    grid.Items.RemoveAt(_index);
                    grid.EIBlock.SetRow(_index, row);
                    grid.SelectedIndex = _index;
                }

                ClearDataField(region, grid);
                this.TempButtonOpen(true);//确定按钮生效
            }
        }

        /// <summary>
        /// 判断合法编号和名称的方法
        /// </summary>
        private Boolean NoAndNameCheck(EFGrid grid, EFDataField name, EFDataField no, string pageName, OperationMode mode)
        {
            String fdNo = no.Value.ToString();
            String fdName = name.Value.ToString();
            Boolean flag = true;
            
            foreach (EIRow code_row in grid.EIBlock.GetRows())
            {
                if (Mode == OperationMode.Update)
                {
                    EIRow rowSelect = (grid.SelectedItem as UIBaseEntity).GetEIRow();
                    if (code_row.Equals(rowSelect))
                    {
                        continue;
                    }
                }
                if (fdNo.Equals(code_row["fdNo"].ToString()))
                {
                    base.OpenEFWindowAlert(pageName + "编号重复！", true);
                    this.TempButtonOpen(true);
                    return false;
                }

                if (fdName.Equals(code_row["fdName"].ToString()))
                {
                    base.OpenEFWindowAlert(pageName + "名称重复！", true);
                    this.TempButtonOpen(true);
                    return false;
                }
            }
            
            return flag;
        }

        /// <summary>
        /// 清空查询区里的值
        /// </summary>
        private void ClearDataField(EFRegion region, EFGrid grid)
        {
            Mode = OperationMode.Insert;
            grid.SelectedItem = null;
            //grid.Refresh();
            foreach (var field in region.GetChildObjects<EFDataField>(null, true))
            {
                field.SetDataValue("");

            }

            //清除列表值得时候设置校验为无效
            ValidatorManager group = this.GetChildObject<ValidatorManager>(null);
            if (group != null)
            {
                group.InValidateAll();
            }
        }

        /// <summary>
        /// 关闭页面
        /// </summary>
        private void ColseWindow()
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }
        #endregion

        #region 矿井信息
        /// <summary>
        /// 矿井信息确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            SaveMineInfor(true);
        }

        private void SaveMineInfor(bool flag)
        {
            if (this.InsertGrid.ValidateField())
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCB01");
                eiInfo.Set("method", "insertMine");

                mineBlock(eiInfo);
                HttpServiceAgent agent = new HttpServiceAgent();
                if (flag)
                {
                    agent.CallService(eiInfo, OnDoWorkCompletedSave);
                }
                else
                {
                    agent.CallService(eiInfo, OnDoWorkCompletedNext);
                }
            }
        }

        /// <summary>
        /// 组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void mineBlock(EIInfo eiInfo)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertGrid.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;

            EIBlock mineBlock = new EIBlock("mineBlock");
            mineBlock.SetColumns(base.GetDataFieldColumns("mineBlock"));
            mineBlock.AddRow(base.GetDataFieldValues("mineBlock"));
            eiInfo.AddBlock(mineBlock);
        }

        /// <summary>
        /// 新增后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedSave(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                CmesWindows _window = this.Parent as CmesWindows;
                _window.DialogResult = true;
                _window.Close();
            }
        }

        /// <summary>
        /// 下一步后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedNext(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            this.EIDataContext.Update(inInfo);
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                //隐藏上帧页面
                this.InsertGrid.Visibility = Visibility.Collapsed;//主表录入
                this.Next.Visibility = Visibility.Collapsed;  //下一步按钮
                this.Cancel.Visibility = Visibility.Collapsed; //关闭按钮
                this.Commit.Visibility = Visibility.Collapsed; //确定按钮
                if (inInfo.Get("value").Equals("0"))
                {
                    //展示水平页面
                    this.HRESULT.Visibility = Visibility.Visible;
                    this.HDETAILS.Visibility = Visibility.Visible;

                    this.SaveH.Visibility = Visibility.Visible;
                    this.ClearH.Visibility = Visibility.Visible;
                    this.LastH.Visibility = Visibility.Visible;
                    this.CommitH.Visibility = Visibility.Visible;
                    this.NextH.Visibility = Visibility.Visible;
                    this.ReturnH.Visibility = Visibility.Visible;
                    this.fdIsFirstH.Value = "否";
                }
                else if (inInfo.Get("value").Equals("1"))
                {
                    //展示采区页面
                    this.MARESULT.Visibility = Visibility.Visible;
                    this.MADETAILS.Visibility = Visibility.Visible;

                    this.SaveMA.Visibility = Visibility.Visible;
                    this.ClearMA.Visibility = Visibility.Visible;
                    this.LastMA.Visibility = Visibility.Visible;
                    this.CommitMA.Visibility = Visibility.Visible;
                    this.NextMA.Visibility = Visibility.Visible;
                    this.ReturnMA.Visibility = Visibility.Visible;
                    this.fdIsStratifiedMA.Value = "否";
                    this.fdIsWingsMA.Value = "否";
                    this.fdIsFirstMA.Value = "否";
                    this.fdParentIdMA.Value = inInfo.GetBlock("mineBlock").GetRow(0)["fdKeyId"];
                }
                else
                {
                    //展示煤层页面
                    this.CRESULT.Visibility = Visibility.Visible;
                    this.CDETAILS.Visibility = Visibility.Visible;

                    this.SaveC.Visibility = Visibility.Visible;
                    this.ClearC.Visibility = Visibility.Visible;
                    this.LastC.Visibility = Visibility.Visible;
                    this.CommitC.Visibility = Visibility.Visible;
                    this.NextC.Visibility = Visibility.Visible;
                    this.ReturnC.Visibility = Visibility.Visible;
                }
                ClearDataField(HDETAILS, h_grid);
                Mode = OperationMode.Insert;
            }
        }


        /// <summary>
        /// 矿井信息下一步按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Next_Click(object sender, RoutedEventArgs e)
        {
            SaveMineInfor(false);
        }

        /// <summary>
        /// 矿井信息取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        #endregion

        #region 水平信息
        /// <summary>
        /// 水平信息保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveH_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            if (this.fdIsFirstH.Value !=null && this.fdIsFirstH.Value.ToString().Equals("是"))
            {
                if (this.h_grid != null)
                {
                    for (int i = 0; i < this.h_grid.EIBlock.GetRowCount(); i++)
                    {
                        this.h_grid.EIBlock.Set(i, "fdIsFirst", "否");
                    }
                }
            }
            saveClick(this.HDETAILS,"Group1", this.h_grid, this.fdNameH, this.fdNoH, "水平");
        }




        /// <summary>
        /// “新增”按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearH_Click(object sender, RoutedEventArgs e)
        {
            // this.h_grid.SelectedItem = null;
            ClearDataField(HDETAILS, h_grid);
        }

        /// <summary>
        /// 水平信息“确定”按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitH_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            SaveHorizontal(true);

        }

        /// <summary>
        /// 水平信息保存公用方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveHorizontal(bool flag)
        {
            if (this.h_grid.EIBlock.GetRowCount() > 0)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCB01");
                eiInfo.Set("method", "insertH");

                //主表新增block
                this.HBlock(eiInfo);
                HttpServiceAgent agent = new HttpServiceAgent();
                if (flag)
                {
                    agent.CallService(eiInfo, OnDoWorkCompletedH);
                }
                else
                {
                    agent.CallService(eiInfo, OnDoWorkCompletedHNext);
                }

            }
            else
            {
                base.OpenEFWindowAlert("请添加水平明细信息！", true);
                this.TempButtonOpen(true);
                return;
            }
        }

        /// <summary>
        /// 水平信息新增后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedH(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 水平信息下一步后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedHNext(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            this.EIDataContext.Update(inInfo);
            this.TempButtonOpen(true);

            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                //隐藏帧页面
                this.HRESULT.Visibility = Visibility.Collapsed;
                this.HDETAILS.Visibility = Visibility.Collapsed;

                this.SaveH.Visibility = Visibility.Collapsed;
                this.ClearH.Visibility = Visibility.Collapsed;
                this.LastH.Visibility = Visibility.Collapsed;
                this.CommitH.Visibility = Visibility.Collapsed;
                this.NextH.Visibility = Visibility.Collapsed;
                this.ReturnH.Visibility = Visibility.Collapsed;

                //显示帧页面
                this.MARESULT.Visibility = Visibility.Visible;
                this.MADETAILS.Visibility = Visibility.Visible;

                this.SaveMA.Visibility = Visibility.Visible;
                this.ClearMA.Visibility = Visibility.Visible;
                this.LastMA.Visibility = Visibility.Visible;
                this.CommitMA.Visibility = Visibility.Visible;
                this.NextMA.Visibility = Visibility.Visible;
                this.ReturnMA.Visibility = Visibility.Visible;
                ClearDataField(HDETAILS, h_grid);
                ClearDataField(MADETAILS, ma_grid);
                this.fdIsStratifiedMA.Value = "否";
                this.fdIsWingsMA.Value = "否";
                this.fdIsFirstMA.Value = "否";
                Mode = OperationMode.Insert;
            }
        }

        /// <summary>
        /// 将列表中的记录组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void HBlock(EIInfo eiInfo)
        {
            EIBlock hBlock = new EIBlock("HBlock");
            hBlock.SetColumns(this.h_grid.EIBlock.GetColumns());
            hBlock.SetRows(this.h_grid.EIBlock.GetRows());
            eiInfo.AddBlock(hBlock);
        }

        /// <summary>
        /// 水平信息上一步
        /// </summary>
        /// <param name="eiInfo"></param>
        private void LastH_Click(object sender, RoutedEventArgs e)
        {
        //    Button button = sender as Button;
        //    button.IsEnabled = false;
        //    TempButton = button;
        //    EIInfo eiInfo = new EIInfo();
        //    eiInfo.Set("service", "ZMCB01");
        //    eiInfo.Set("method", "deleteMine");
        //    HttpServiceAgent agent = new HttpServiceAgent();
        //    agent.CallService(eiInfo, OnDoWorkCompletedDeleteH);
           
        //}

        ///// <summary>
        ///// 水平信息上一步后的回调方法
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void OnDoWorkCompletedDeleteH(object sender, DoWorkCompletedEventArgs e)
        //{
        //    EIInfo inInfo = e.EIInfo;
        //    this.TempButtonOpen(true);

        //    if (inInfo.Status == -1)
        //    {
        //        this.TempButtonOpen(true);
        //        this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
        //    }
        //    else
        //    {
                //隐藏上帧页面
                this.InsertGrid.Visibility = Visibility.Visible;//主表录入
                this.Next.Visibility = Visibility.Visible;  //下一步按钮
                this.Cancel.Visibility = Visibility.Visible; //关闭按钮
                this.Commit.Visibility = Visibility.Visible; //确定按钮

                //展示下帧页面
                this.HRESULT.Visibility = Visibility.Collapsed;
                this.HDETAILS.Visibility = Visibility.Collapsed;

                this.SaveH.Visibility = Visibility.Collapsed;
                this.ClearH.Visibility = Visibility.Collapsed;
                this.LastH.Visibility = Visibility.Collapsed;
                this.CommitH.Visibility = Visibility.Collapsed;
                this.NextH.Visibility = Visibility.Collapsed;
                this.ReturnH.Visibility = Visibility.Collapsed;
                Mode = OperationMode.Insert;
            //}
        }

        /// <summary>
        /// 水平信息下一步
        /// </summary>
        /// <param name="eiInfo"></param>
        private void NextH_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            SaveHorizontal(false);
        }

        /// <summary>
        /// 水平信息关闭
        /// </summary>
        /// <param name="eiInfo"></param>
        private void ReturnH_Click(object sender, RoutedEventArgs e)
        {
            ColseWindow();
        }

        /// <summary>
        /// 水平信息“删除”链接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteButtonH_Click(object sender, RoutedEventArgs e)
        {
            base.TempHyperlink = sender as HyperlinkButton;
            EIRow row = (base.TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            if (value.Value.ToString().Equals("0"))
            {
                if (this.ma_grid.EIBlock != null)
                {
                    for (int i = 0; i < this.ma_grid.EIBlock.GetRowCount(); i++)
                    {
                        if (this.ma_grid.EIBlock.GetRow(i)["fdParentId"].ToString().Equals(row["fdKeyId"].ToString()))
                        {
                            base.OpenEFWindowAlert("该水平已经被引用，不能删除!", true);
                            return;
                        }
                    }
                }
            }
            DialogParameters param = this.ModelDialogParameters("Confirm", "确定删除所选择的记录？");
            param.Closed = DeleteConfirmClosed;
            EFWindow.Confirm(param);
        }

        /// <summary>
        /// 点击确认按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null)
            {
                return;
            }
            if (e.DialogResult.Value)
            {
                EIRow row = (base.TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
                this.h_grid.EIBlock.Rows.Remove(row);
                ClearDataField(HDETAILS, h_grid);
            }
        }


        /// <summary>
        /// 选中水平信息列表一条信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void recordGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (this.h_grid.SelectedItem != null)
            {
                Mode = OperationMode.Update;
                var _item = this.h_grid.SelectedItem;
                foreach (var _field in this.HDETAILS.GetChildObjects<EFDataField>(null, true))
                {
                    _field.Value = _item.GetType().GetProperty(_field.eBindingName).GetValue(_item, null);
                }
            }
        }

        #endregion

        #region 采区信息

        /// <summary>
        /// 采区信息保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveMA_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            if (this.fdIsFirstMA.Value !=null && this.fdIsFirstMA.Value.ToString().Equals("是"))
            {
                if (this.ma_grid != null)
                {
                    for (int i = 0; i < this.ma_grid.EIBlock.GetRowCount(); i++)
                    {
                        this.ma_grid.EIBlock.Set(i, "fdIsFirst", "否");
                    }
                }
            }
            saveClick(this.MADETAILS,"Group2", this.ma_grid, this.fdNameMA, this.fdNoMA, "采区");
        }

        /// <summary>
        /// 采区信息清空按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearMA_Click(object sender, RoutedEventArgs e)
        {
            ClearDataField(MADETAILS, ma_grid);
        }


        /// <summary>
        /// 采区信息上一步按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LastMA_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCB01");
            eiInfo.Set("method", "queryHOrMine");
            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, OnDoWorkCompletedDeleteMA);
        }

        /// <summary>
        /// 采区信息上一步回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedDeleteMA(object sender, DoWorkCompletedEventArgs e)
        {
             EIInfo inInfo = e.EIInfo;

            this.TempButtonOpen(true);

            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                //隐藏帧页面
                this.MARESULT.Visibility = Visibility.Collapsed;
                this.MADETAILS.Visibility = Visibility.Collapsed;

                this.SaveMA.Visibility = Visibility.Collapsed;
                this.ClearMA.Visibility = Visibility.Collapsed;
                this.LastMA.Visibility = Visibility.Collapsed;
                this.CommitMA.Visibility = Visibility.Collapsed;
                this.NextMA.Visibility = Visibility.Collapsed;
                this.ReturnMA.Visibility = Visibility.Collapsed;
                if (this.value.Value.ToString().Equals("0"))
                {
                    //展示上帧水平页面
                    this.h_grid.UpdateData(inInfo);
                    this.HRESULT.Visibility = Visibility.Visible;
                    this.HDETAILS.Visibility = Visibility.Visible;

                    this.SaveH.Visibility = Visibility.Visible;
                    this.ClearH.Visibility = Visibility.Visible;
                    this.LastH.Visibility = Visibility.Visible;
                    this.CommitH.Visibility = Visibility.Visible;
                    this.NextH.Visibility = Visibility.Visible;
                    this.ReturnH.Visibility = Visibility.Visible;
                    ClearDataField(HDETAILS, h_grid);
                }
                else
                {
                    //展示上帧矿井信息页面
                    this.InsertGrid.Visibility = Visibility.Visible;//主表录入
                    this.Next.Visibility = Visibility.Visible;  //下一步按钮
                    this.Cancel.Visibility = Visibility.Visible; //关闭按钮
                    this.Commit.Visibility = Visibility.Visible; //确定按钮
                }
                Mode = OperationMode.Insert;
            }
        }

        /// <summary>
        /// 采区信息下一步按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NextMA_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            SaveMineArea(false);
        }


        /// <summary>
        /// 采区信息返回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReturnMA_Click(object sender, RoutedEventArgs e)
        {
            ColseWindow();
        }

        /// <summary>
        /// 采区信息确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitMA_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            SaveMineArea(true);

        }

        /// <summary>
        /// 采区信息保存公用方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveMineArea(bool flag)
        {
            if (this.ma_grid.EIBlock.GetRowCount() > 0)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCB01");
                eiInfo.Set("method", "insertMA");

                //主表新增block
                this.MABlock(eiInfo);
                HttpServiceAgent agent = new HttpServiceAgent();
                if (flag)
                {
                    agent.CallService(eiInfo, OnDoWorkCompletedMA);
                }
                else
                {
                    agent.CallService(eiInfo, OnDoWorkCompletedMANext);
                }

            }
            else
            {
                base.OpenEFWindowAlert("请添加采区明细信息！", true);
                this.TempButtonOpen(true);
                return;
            }
        }

        /// <summary>
        /// 将列表中的记录组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void MABlock(EIInfo eiInfo)
        {
            EIBlock maBlock = new EIBlock("MABlock");
            maBlock.SetColumns(this.ma_grid.EIBlock.GetColumns());
            maBlock.SetRows(this.ma_grid.EIBlock.GetRows());
            eiInfo.AddBlock(maBlock);
        }

        /// <summary>
        /// 采区信息新增后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedMA(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 采区信息下一步后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedMANext(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            this.EIDataContext.Update(inInfo);
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                //显示帧页面
                this.CRESULT.Visibility = Visibility.Visible;
                this.CDETAILS.Visibility = Visibility.Visible;

                this.SaveC.Visibility = Visibility.Visible;
                this.ClearC.Visibility = Visibility.Visible;
                this.LastC.Visibility = Visibility.Visible;
                this.CommitC.Visibility = Visibility.Visible;
                this.NextC.Visibility = Visibility.Visible;
                this.ReturnC.Visibility = Visibility.Visible;

                //隐藏帧页面
                this.MARESULT.Visibility = Visibility.Collapsed;
                this.MADETAILS.Visibility = Visibility.Collapsed;

                this.SaveMA.Visibility = Visibility.Collapsed;
                this.ClearMA.Visibility = Visibility.Collapsed;
                this.LastMA.Visibility = Visibility.Collapsed;
                this.CommitMA.Visibility = Visibility.Collapsed;
                this.NextMA.Visibility = Visibility.Collapsed;
                this.ReturnMA.Visibility = Visibility.Collapsed;
                this.TempButtonOpen(true);
                ClearDataField(MADETAILS, ma_grid);
                ClearDataField(CDETAILS, c_grid);
                Mode = OperationMode.Insert;
            }
        }



        /// <summary>
        /// 采区信息“删除”链接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteButtonMA_Click(object sender, RoutedEventArgs e)
        {
            base.TempHyperlink = sender as HyperlinkButton;
            EIRow row = (base.TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            if (value.Value.ToString().Equals("0") || value.Value.ToString().Equals("1"))
            {
                if (this.cm_grid.EIBlock != null)
                {
                    for (int i = 0; i < this.cm_grid.EIBlock.GetRowCount(); i++)
                    {
                        if (this.cm_grid.EIBlock.GetRow(i)["fdParentId"].ToString().Equals(row["fdKeyId"].ToString()))
                        {
                            base.OpenEFWindowAlert("该采区已经被引用，不能删除!", true);
                            return;
                        }
                    }
                }
            }
            DialogParameters param = this.ModelDialogParameters("Confirm", "确定删除所选择的记录？");
            param.Closed = DeleteConfirmMAClosed;
            EFWindow.Confirm(param);
        }

        /// <summary>
        /// 点击确认按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteConfirmMAClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null)
            {
                return;
            }
            if (e.DialogResult.Value)
            {
                EIRow row = (base.TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
                this.ma_grid.EIBlock.Rows.Remove(row);
                ClearDataField(MADETAILS, ma_grid);
            }
        }


        /// <summary>
        /// 选中采区信息列表一条信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ma_grid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (this.ma_grid.SelectedItem != null)
            {
                Mode = OperationMode.Update;
                var _item = this.ma_grid.SelectedItem;
                foreach (var _field in this.MADETAILS.GetChildObjects<EFDataField>(null, true))
                {
                    _field.Value = _item.GetType().GetProperty(_field.eBindingName).GetValue(_item, null);
                }
            }
        }

        #endregion

        #region 煤层信息

        /// <summary>
        /// 煤层信息保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveC_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            saveClick(this.CDETAILS, "Group3", this.c_grid, this.fdNameC, this.fdNoC, "煤层");
        }

        /// <summary>
        /// 煤层信息新增按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearC_Click(object sender, RoutedEventArgs e)
        {
            ClearDataField(CDETAILS, c_grid);
        }

        /// <summary>
        /// 煤层信息确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitC_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            SaveCoalseam(true);
        }

        /// <summary>
        /// 煤层信息保存公用方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveCoalseam(bool flag)
        {
            if (this.c_grid.EIBlock.GetRowCount() > 0)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCB01");
                eiInfo.Set("method", "insertCoalseam");

                //主表新增block
                this.CBlock(eiInfo);
                HttpServiceAgent agent = new HttpServiceAgent();
                if (flag)
                {
                    agent.CallService(eiInfo, OnDoWorkCompletedC);
                }
                else
                {
                    agent.CallService(eiInfo, OnDoWorkCompletedCNext);
                }

            }
            else
            {
                base.OpenEFWindowAlert("请添加煤层明细信息！", true);
                this.TempButtonOpen(true);
                return;
            }
        }

        /// <summary>
        /// 煤层信息下一步后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedCNext(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            this.EIDataContext.Update(inInfo);
            this.TempButtonOpen(true);
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                //隐藏帧页面
                this.CRESULT.Visibility = Visibility.Collapsed;
                this.CDETAILS.Visibility = Visibility.Collapsed;

                this.SaveC.Visibility = Visibility.Collapsed;
                this.ClearC.Visibility = Visibility.Collapsed;
                this.LastC.Visibility = Visibility.Collapsed;
                this.CommitC.Visibility = Visibility.Collapsed;
                this.NextC.Visibility = Visibility.Collapsed;
                this.ReturnC.Visibility = Visibility.Collapsed;

                //显示帧页面
                this.CMRESULT.Visibility = Visibility.Visible;
                this.CMDETAILS.Visibility = Visibility.Visible;

                this.SaveCM.Visibility = Visibility.Visible;
                this.ClearCM.Visibility = Visibility.Visible;
                this.LastCM.Visibility = Visibility.Visible;
                this.CommitCM.Visibility = Visibility.Visible;
                this.NextCM.Visibility = Visibility.Visible;
                this.ReturnCM.Visibility = Visibility.Visible;
                this.fdStatusCM.Value = "设计";
                this.fdIsFirstCM.Value = "否";
                ClearDataField(CDETAILS, c_grid);
                ClearDataField(CMDETAILS, cm_grid);
                Mode = OperationMode.Insert;
            }
        }

        /// <summary>
        /// 煤层信息新增后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedC(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 将列表中的记录组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void CBlock(EIInfo eiInfo)
        {
            EIBlock cBlock = new EIBlock("CBlock");
            cBlock.SetColumns(this.c_grid.EIBlock.GetColumns());
            cBlock.SetRows(this.c_grid.EIBlock.GetRows());
            eiInfo.AddBlock(cBlock);
        }

        /// <summary>
        /// 煤层信息"上一步"按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LastC_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCB01");
            eiInfo.Set("method", "queryMaOrMine");
            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, OnDoWorkCompletedDeleteMaOrMine);
        }

        /// <summary>
        /// 煤层信息上一步回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedDeleteMaOrMine(object sender, DoWorkCompletedEventArgs e)
        {
             EIInfo inInfo = e.EIInfo;
            this.TempButtonOpen(true);

            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                 //隐藏帧页面
                this.CRESULT.Visibility = Visibility.Collapsed;
                this.CDETAILS.Visibility = Visibility.Collapsed;

                this.SaveC.Visibility = Visibility.Collapsed;
                this.ClearC.Visibility = Visibility.Collapsed;
                this.LastC.Visibility = Visibility.Collapsed;
                this.CommitC.Visibility = Visibility.Collapsed;
                this.NextC.Visibility = Visibility.Collapsed;
                this.ReturnC.Visibility = Visibility.Collapsed;

                if (this.value.Value.ToString().Equals("0") || this.value.Value.ToString().Equals("1"))
                {
                    //显示采区帧页面
                    this.ma_grid.UpdateData(inInfo);
                    this.MARESULT.Visibility = Visibility.Visible;
                    this.MADETAILS.Visibility = Visibility.Visible;

                    this.SaveMA.Visibility = Visibility.Visible;
                    this.ClearMA.Visibility = Visibility.Visible;
                    this.LastMA.Visibility = Visibility.Visible;
                    this.CommitMA.Visibility = Visibility.Visible;
                    this.NextMA.Visibility = Visibility.Visible;
                    this.ReturnMA.Visibility = Visibility.Visible;
                    ClearDataField(MADETAILS, ma_grid);
                    Mode = OperationMode.Insert;
                }
                else
                {
                    //展示上帧矿井信息页面
                    this.InsertGrid.Visibility = Visibility.Visible;//主表录入
                    this.Next.Visibility = Visibility.Visible;  //下一步按钮
                    this.Cancel.Visibility = Visibility.Visible; //关闭按钮
                    this.Commit.Visibility = Visibility.Visible; //确定按钮
                }
            }
        }

        /// <summary>
        /// 煤层信息"下一步"按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NextC_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            SaveCoalseam(false);
        }

        /// <summary>
        /// 煤层信息"返回"按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReturnC_Click(object sender, RoutedEventArgs e)
        {
            ColseWindow();
        }

        /// <summary>
        /// 煤层信息"删除"按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteButtonC_Click(object sender, RoutedEventArgs e)
        {
            base.TempHyperlink = sender as HyperlinkButton;
            EIRow row = (base.TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            if (this.cm_grid.EIBlock != null)
            {
                for (int i = 0; i < this.cm_grid.EIBlock.GetRowCount(); i++)
                {
                    if (this.cm_grid.EIBlock.GetRow(i)["fdCoalseamId"].ToString().Equals(row["fdKeyId"].ToString()))
                    {
                        base.OpenEFWindowAlert("该煤层已经被引用，不能删除!", true);
                        return;
                    }
                }
            }
           
            DialogParameters param = this.ModelDialogParameters("Confirm", "确定删除所选择的记录？");
            param.Closed = DeleteConfirmCClosed;
            EFWindow.Confirm(param);
        }

        /// <summary>
        /// 点击确认按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteConfirmCClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null)
            {
                return;
            }
            if (e.DialogResult.Value)
            {
                EIRow row = (base.TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
                this.c_grid.EIBlock.Rows.Remove(row);
                ClearDataField(CDETAILS, c_grid);
            }
        }

        /// <summary>
        /// 选中煤层信息列表一条信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void c_grid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (this.c_grid.SelectedItem != null)
            {
                Mode = OperationMode.Update;
                var _item = this.c_grid.SelectedItem;
                foreach (var _field in this.CDETAILS.GetChildObjects<EFDataField>(null, true))
                {
                    _field.Value = _item.GetType().GetProperty(_field.eBindingName).GetValue(_item, null);
                }
            }
        }

        #endregion

        #region 采面信息

        /// <summary>
        /// 采面信息保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveCM_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            if (this.fdIsFirstCM.Value !=null && this.fdIsFirstCM.Value.ToString().Equals("是"))
            {
                if (this.cm_grid != null)
                {
                    for (int i = 0; i < this.cm_grid.EIBlock.GetRowCount(); i++)
                    {
                        this.cm_grid.EIBlock.Set(i, "fdIsFirst", "否");
                    }
                }
            }
            saveClick(this.CMDETAILS, "Group4", this.cm_grid, this.fdNameCM, this.fdNoCM, "采面");
        }

        /// <summary>
        /// 采面信息新增按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearCM_Click(object sender, RoutedEventArgs e)
        {
            ClearDataField(CMDETAILS, cm_grid);
        }


        /// <summary>
        /// 采面信息确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitCM_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            SaveWorkingface(true);
        }

        /// <summary>
        /// 采区信息保存公用方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveWorkingface(bool flag)
        {
            if (this.cm_grid.EIBlock.GetRowCount() > 0)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCB01");
                eiInfo.Set("method", "insertWorkingface");

                //主表新增block
                this.CMBlock(eiInfo);
                HttpServiceAgent agent = new HttpServiceAgent();
                if (flag)
                {
                    agent.CallService(eiInfo, OnDoWorkCompletedCM);
                }
                else
                {
                    agent.CallService(eiInfo, OnDoWorkCompletedCMNext);
                }

            }
            else
            {
                base.OpenEFWindowAlert("请添加采面明细信息！", true);
                this.TempButtonOpen(true);
                return;
            }
        }

        /// <summary>
        /// 将列表中的记录组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void CMBlock(EIInfo eiInfo)
        {
            EIBlock cmBlock = new EIBlock("CMBlock");
            cmBlock.SetColumns(this.cm_grid.EIBlock.GetColumns());
            cmBlock.SetRows(this.cm_grid.EIBlock.GetRows());
            eiInfo.AddBlock(cmBlock);
        }

        /// <summary>
        /// 采面信息新增后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedCM(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 采面信息下一步后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedCMNext(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            this.EIDataContext.Update(inInfo);
            this.TempButtonOpen(true);
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                //显示帧页面
                this.RRESULT.Visibility = Visibility.Visible;
                this.RDETAILS.Visibility = Visibility.Visible;

                this.SaveR.Visibility = Visibility.Visible;
                this.ClearR.Visibility = Visibility.Visible;
                this.LastR.Visibility = Visibility.Visible;
                this.CommitR.Visibility = Visibility.Visible;
                this.ReturnR.Visibility = Visibility.Visible;

                //隐藏帧页面
                this.CMRESULT.Visibility = Visibility.Collapsed;
                this.CMDETAILS.Visibility = Visibility.Collapsed;

                this.SaveCM.Visibility = Visibility.Collapsed;
                this.ClearCM.Visibility = Visibility.Collapsed;
                this.LastCM.Visibility = Visibility.Collapsed;
                this.CommitCM.Visibility = Visibility.Collapsed;
                this.NextCM.Visibility = Visibility.Collapsed;
                this.ReturnCM.Visibility = Visibility.Collapsed;
                ClearDataField(CMDETAILS, cm_grid);
                ClearDataField(RDETAILS, r_grid);
                Mode = OperationMode.Insert;
            }
        }

        /// <summary>
        /// 采面信息"上一步"按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LastCM_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCB01");
            eiInfo.Set("method", "queryC");
            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, OnDoWorkCompletedDeleteC);
        }

        /// <summary>
        /// 采面信息上一步回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedDeleteC(object sender, DoWorkCompletedEventArgs e)
        {
             EIInfo inInfo = e.EIInfo;
            this.TempButtonOpen(true);

            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                //隐藏帧页面
                this.c_grid.UpdateData(inInfo);
                this.CMRESULT.Visibility = Visibility.Collapsed;
                this.CMDETAILS.Visibility = Visibility.Collapsed;

                this.SaveCM.Visibility = Visibility.Collapsed;
                this.ClearCM.Visibility = Visibility.Collapsed;
                this.LastCM.Visibility = Visibility.Collapsed;
                this.CommitCM.Visibility = Visibility.Collapsed;
                this.NextCM.Visibility = Visibility.Collapsed;
                this.ReturnCM.Visibility = Visibility.Collapsed;

                //显示帧页面
                this.CRESULT.Visibility = Visibility.Visible;
                this.CDETAILS.Visibility = Visibility.Visible;

                this.SaveC.Visibility = Visibility.Visible;
                this.ClearC.Visibility = Visibility.Visible;
                this.LastC.Visibility = Visibility.Visible;
                this.CommitC.Visibility = Visibility.Visible;
                this.NextC.Visibility = Visibility.Visible;
                this.ReturnC.Visibility = Visibility.Visible;
                ClearDataField(CDETAILS, c_grid);
                Mode = OperationMode.Insert;
            }
        }

        /// <summary>
        /// 采面信息"下一步"按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NextCM_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            SaveWorkingface(false);
        }

        /// <summary>
        /// 采面信息"返回"按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReturnCM_Click(object sender, RoutedEventArgs e)
        {
            ColseWindow();
        }
        private void DeleteButtonCM_Click(object sender, RoutedEventArgs e)
        {
            base.TempHyperlink = sender as HyperlinkButton;
            DialogParameters param = this.ModelDialogParameters("Confirm", "确定删除所选择的记录？");
            param.Closed = DeleteConfirmCMClosed;
            EFWindow.Confirm(param);
        }

        /// <summary>
        /// 点击确认按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteConfirmCMClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null)
            {
                return;
            }
            if (e.DialogResult.Value)
            {
                EIRow row = (base.TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
                this.cm_grid.EIBlock.Rows.Remove(row);
                ClearDataField(CMDETAILS, cm_grid);
            }
        }

        /// <summary>
        /// 选中采面信息列表一条信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cm_grid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (this.cm_grid.SelectedItem != null)
            {
                Mode = OperationMode.Update;
                var _item = this.cm_grid.SelectedItem;
                foreach (var _field in this.CMDETAILS.GetChildObjects<EFDataField>(null, true))
                {
                    _field.Value = _item.GetType().GetProperty(_field.eBindingName).GetValue(_item, null);
                }
            }
        }

        /// <summary>
        /// 采高
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdHeight_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
        }

        /// <summary>
        /// 平均长度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdLength_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
            this.fdSurplusToLengthCM.Value = fdLengthCM.Value.ToString();
        }
        /// <summary>
        /// 宽度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdWidth_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
        }

        /// <summary>
        /// 容重
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDensity_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
        }

        /// <summary>
        /// 回采率
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdChanceCM_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
        }

        /// <summary>
        /// 剩余走向
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdSurplusToLength_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            GetMineData();
        }

        /// <summary>
        /// 计算储量=平均长度*采高*宽度*容重*回采率/100
        /// 计算剩余长度=平均长度-0
        /// 计算剩余储量=剩余长度*采高*宽度*容重*回采率/100
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param
        private void GetMineData()
        {
            double height = 0.0, length = 0.0, width = 0.0, density = 0.0, chance = 0.0, surplusToLength = 0.0;
            if (fdHeightCM.Value != null)
            {
                Double.TryParse(fdHeightCM.Value.ToString(), out height);
            }
            if (fdLengthCM.Value != null)
            {
                Double.TryParse(fdLengthCM.Value.ToString(), out length);
            }
            if (fdWidthCM.Value != null)
            {
                Double.TryParse(fdWidthCM.Value.ToString(), out width);
            }
            if (fdDensityCM.Value != null)
            {
                Double.TryParse(fdDensityCM.Value.ToString(), out density);
            }
            if (fdChanceCM.Value != null)
            {
                Double.TryParse(fdChanceCM.Value.ToString(), out chance);
            }
            if (fdSurplusToLengthCM.Value != null)
            {
                Double.TryParse(fdSurplusToLengthCM.Value.ToString(), out surplusToLength);
            }
            this.fdReservesCM.Value = Math.Round(height * length * width * density * chance / 100, 4);
            
            this.fdSurplusReservesCM.Value = Math.Round(height * surplusToLength * width * density * chance / 100, 4);
           
        }


        /// <summary>
        /// 选择所属煤层，带出容重
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdCoalseamIdCM_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox selectedData = (sender as EFDataField).Content as EFComboBox;
            if ((selectedData.SelectedItem as EIEntity) != null)
            {
                EIRow selectedRow = (selectedData.SelectedItem as EIEntity).GetEIRow();
                this.fdDensityCM.Value = selectedRow["fdDensity"].ToString();
            }
        }
        #endregion
      
        #region 巷道信息

        /// <summary>
        /// 巷道信息保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveR_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            saveClick(this.RDETAILS, "Group5", this.r_grid, this.fdNameRM, this.fdNoRM, "巷道");
        }

        /// <summary>
        /// 巷道信息清空按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearR_Click(object sender, RoutedEventArgs e)
        {
            ClearDataField(RDETAILS, r_grid);
        }

        private void CommitR_Click(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            SaveRoadWay(true);
        }

        /// <summary>
        /// 巷道信息保存公用方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveRoadWay(bool flag)
        {
            if (this.r_grid.EIBlock.GetRowCount() > 0)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCB01");
                eiInfo.Set("method", "insertRoadWay");

                //主表新增block
                this.RWBlock(eiInfo);
                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, OnDoWorkCompletedRW);
            }
            else
            {
                base.OpenEFWindowAlert("请添加巷道明细信息！", true);
                this.TempButtonOpen(true);
                return;
            }
        }

        /// <summary>
        /// 将列表中的记录组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void RWBlock(EIInfo eiInfo)
        {
            EIBlock rwBlock = new EIBlock("RWBlock");
            rwBlock.SetColumns(this.r_grid.EIBlock.GetColumns());
            rwBlock.SetRows(this.r_grid.EIBlock.GetRows());
            eiInfo.AddBlock(rwBlock);
        }


        /// <summary>
        /// 巷道信息新增后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedRW(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }


        /// <summary>
        /// 巷道信息上一步按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LastR_Click(object sender, RoutedEventArgs e)
        {
             Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCB01");
            eiInfo.Set("method", "queryCM");
            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, OnDoWorkCompletedDeleteCM);
        }

        /// <summary>
        /// 巷道信息上一步回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompletedDeleteCM(object sender, DoWorkCompletedEventArgs e)
        {
             EIInfo inInfo = e.EIInfo;
            this.TempButtonOpen(true);

            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                //隐藏帧页面
                this.RRESULT.Visibility = Visibility.Collapsed;
                this.RDETAILS.Visibility = Visibility.Collapsed;

                this.SaveR.Visibility = Visibility.Collapsed;
                this.ClearR.Visibility = Visibility.Collapsed;
                this.LastR.Visibility = Visibility.Collapsed;
                this.CommitR.Visibility = Visibility.Collapsed;
                this.ReturnR.Visibility = Visibility.Collapsed;

                //显示帧页面
                this.cm_grid.UpdateData(inInfo);
                this.CMRESULT.Visibility = Visibility.Visible;
                this.CMDETAILS.Visibility = Visibility.Visible;

                this.SaveCM.Visibility = Visibility.Visible;
                this.ClearCM.Visibility = Visibility.Visible;
                this.LastCM.Visibility = Visibility.Visible;
                this.CommitCM.Visibility = Visibility.Visible;
                this.NextCM.Visibility = Visibility.Visible;
                this.ReturnCM.Visibility = Visibility.Visible;
                ClearDataField(CMDETAILS, cm_grid);
                Mode = OperationMode.Insert;
            }
        }
             
        /// <summary>
        /// 巷道信息返回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReturnR_Click(object sender, RoutedEventArgs e)
        {
            ColseWindow();
        }

        /// <summary>
        /// 点击确认按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteButtonR_Click(object sender, RoutedEventArgs e)
        {
            base.TempHyperlink = sender as HyperlinkButton;
            DialogParameters param = this.ModelDialogParameters("Confirm", "确定删除所选择的记录？");
            param.Closed = DeleteConfirmRClosed;
            EFWindow.Confirm(param);
        }

        /// <summary>
        /// 点击确认按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteConfirmRClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null)
            {
                return;
            }
            if (e.DialogResult.Value)
            {
                EIRow row = (base.TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
                this.r_grid.EIBlock.Rows.Remove(row);
                ClearDataField(RDETAILS, r_grid);
            }
        }

        /// <summary>
        /// 选中巷道信息列表一条信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void r_grid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (this.r_grid.SelectedItem != null)
            {
                Mode = OperationMode.Update;
                var _item = this.r_grid.SelectedItem;
                foreach (var _field in this.RDETAILS.GetChildObjects<EFDataField>(null, true))
                {
                    _field.Value = _item.GetType().GetProperty(_field.eBindingName).GetValue(_item, null);
                }
            }
        }

        /// <summary>
        /// 剩余进尺随设计进尺而变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdLengthRM_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            fdSurplusLengthRM.Value = fdLengthRM.Value;
        }

        private void fdParentIdRM_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = sender as EFDataField;
            EFComboBox box = field.Content as EFComboBox;
            if (field.GetDataValue() !=null)
            {
                if (!field.GetDataValue().Equals(""))
                {
                    var result = from c in box.Items where c.GetType().GetProperty("value").GetValue(c, null).ToString().Equals(field.Value.ToString()) select c;

                    if (result.Count() > 0)
                    {
                        object Item = result.First();
                        this.fdParentTypeRM.Value = Item.GetType().GetProperty("type").GetValue(Item, null);
                        this.fdParentIdRM.Value = Item.GetType().GetProperty("value").GetValue(Item, null);
                        //根据所属区域类型和巷道用途来设置所属工序
                        if (this.fdRoadwayUsingRM.Value != null && !"".Equals(this.fdRoadwayUsingRM.Value.ToString()))
                        {
                            CoalseamOfProcessJudge coalOfProcJudge = new CoalseamOfProcessJudge();
                            this.fdProcessRM.Value = coalOfProcJudge.JudgeProcess(this.fdParentTypeRM.Value.ToString(), this.fdRoadwayUsingRM.Value.ToString());
                        }
                    }
                }
            }
        }

        private void fdRoadwayUsingRM_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            //根据所属区域类型和巷道用途来设置所属工序
            if (this.fdParentTypeRM.Value != null && !"".Equals(this.fdParentTypeRM.Value.ToString()))
            {
                CoalseamOfProcessJudge coalOfProcJudge = new CoalseamOfProcessJudge();
                this.fdProcessRM.Value = coalOfProcJudge.JudgeProcess(this.fdParentTypeRM.Value.ToString(), this.fdRoadwayUsingRM.Value.ToString());
            }
        }
    }
        #endregion
}
        
    

